package com.tensquare.user.controller;

import ch.qos.logback.core.pattern.util.RegularEscapeUtil;
import com.tensquare.user.pojo.User;
import com.tensquare.user.service.UserService;
import entity.PageResult;
import entity.Result;
import entity.StatusCode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

/**
 * @Author:zimmor
 * @Date: 2021/3/11 12:05
 * @Description  控制层
 * @version: 1.0.0
 */
@RestController
@CrossOrigin
@RequestMapping("/user")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 取关
     * @param friendid
     * @return
     */
    @RequestMapping(method = RequestMethod.DELETE, value = "/follow/{friendid}")
    public Result cancelAttention(@PathVariable String friendid) {
        String userid = "2";
        userService.cancelAttention(userid, friendid);
        return new Result(true, StatusCode.OK, "取关成功");
    }

    /**
     * 关注某个用户
     * @param friendid
     * @return
     */
    @RequestMapping(method = RequestMethod.PUT, value = "/follow/{friendid}")
    public Result followcountAdd(@PathVariable String friendid) {
        //当前用户
        String userid = "2";
        userService.followcountAdd(userid, friendid);
        return new Result(true, StatusCode.OK, "关注成功");
    }

    /**
     * 发送验证码
     * @param mobile
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/sendsms/{mobile}")
    public Result sendMsm(@PathVariable String mobile) {
        userService.sendMessage(mobile);
        return new Result(true, StatusCode.OK, "发送验证码成功！");
    }

    /**
     * 分页条件查询
     * @param user
     * @param page
     * @param size
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value="/findSearch/{page}/{size}")
    public Result findSearch(@RequestBody User user, @PathVariable int page, @PathVariable int size) {
        Page pageData = userService.findSearch(user, page, size);
        return new Result(true, StatusCode.OK, "查询成功", new PageResult<User>(pageData.getTotalElements(),pageData.getContent()));
    }

    /**
     * 条件查询
     * @param user
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/findSearch")
    public Result findSearch(@RequestBody User user) {
        return new Result(true, StatusCode.OK, "查询成功", userService.findSearch(user));
    }

    /**
     * 查询所有
     * @return
     */
    @RequestMapping(method = RequestMethod.GET)
    public Result findAll() {
        return new Result(true, StatusCode.OK, "查询成功", userService.findAll());
    }
    @RequestMapping(method = RequestMethod.GET, value = "/{id}")
    public Result findById(@PathVariable String id) {
        return new Result(true, StatusCode.OK, "查询成功", userService.findById(id));
    }
    @RequestMapping(method = RequestMethod.POST)
    public Result save (@RequestBody User user) {
        userService.save(user);
        return new Result(true, StatusCode.OK, "新增成功");
    }

    /**
     * 注册
     * @param user
     * @param code
     * @return
     */
    @RequestMapping(method = RequestMethod.POST, value = "/register/{code}")
    public Result save(@RequestBody User user,@PathVariable String code) {
        userService.register(user, code);
        return new Result(true, StatusCode.OK, "注册成功");
    }

    @RequestMapping(method = RequestMethod.PUT, value = "/{id}")
    public Result update(@RequestBody User user, @PathVariable String id) {
        user.setId(id);
        userService.update(user);
        return new Result(true, StatusCode.OK, "修改成功");
    }

    @RequestMapping(method = RequestMethod.DELETE, value = "/{id}")
    public Result deleteById(@PathVariable String id) {
        userService.delete(id);
        return new Result(true, StatusCode.OK, "删除成功");

    }
}
